<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="场站名" prop="czid">
        <el-select filterable clearable v-model="queryParams.czid" placeholder="选择场站名" clearable @change="getBuJian1List" >
          <el-option v-for="namelist in cznameList"
                     :key="namelist.czid"
                     :label="namelist.czname"
                     :value="namelist.czid"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="部件" prop="bjid">
        <el-select filterable clearable v-model="queryParams.bjid" placeholder="选择部件" >
          <el-option v-for="bujianlist in bjList"
                     :key="bujianlist.bjid"
                     :label="bujianlist.bjname"
                     :value="bujianlist.bjid"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="日期" prop="criqi">
        <el-date-picker
          v-model="queryParams.criqi"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM"
          value-format="yyyy-MM-dd"
          clearable
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="部件状态" prop="status">
        <el-select v-model="queryParams.bjpara" placeholder="菜单状态" clearable>
          <el-option
            v-for="dict in dict.type.bjpara_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryZhouQi">周期</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ruoyi-yuce:faultrecord:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport1"
          v-hasPermi="['ruoyi-yuce:endstart:export']"
        >周期数据导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  v-loading="loading" :data="faultrecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="riqi" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.riqi, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="场站名" align="center" prop="czname" />
      <el-table-column label="部件类型" align="center" prop="bjtypename" />
      <el-table-column label="部件名" align="center" prop="bjname" />
      <el-table-column label="部件状态" align="center" prop="bjpara" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bjpara_status" :value="scope.row.bjpara"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改2020年1月故障记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFaultrecord, getFaultrecord, delFaultrecord, addFaultrecord, updateFaultrecord, getchangzhanList, getbujianList, getbujian1List} from "@/api/ruoyi-yuce/faultrecord";
import {listEndstart} from "@/api/ruoyi-yuce/endstart";
import {listKwhdata} from "@/api/ruoyi-yuce/kwhdata";

export default {
  name: "Faultrecord",
  dicts: ['bjpara_status'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 搜索周期还是故障 true为故障，false为周期
      searchFlag: true,
      // 总条数
      total: 0,
      // 2020年1月故障记录表格数据
      faultrecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        czid: null,
        bjid: null,
        criqi: null,
        bjpara:null
      },
      cznameList:[],  //场站名称列表
      bjList:[],   //部件列表
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    console.log("created执行了")
    this.getChangZhanList()
    this.getBuJianList()
  },
  methods: {
    /** 查询记录列表 */
    getList() {
      this.loading = true;
      console.log("查询哪种",this.searchFlag)
      if (this.searchFlag){
        listFaultrecord(this.queryParams).then(response => {
          this.faultrecordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }else {
        listEndstart(this.queryParams).then(response => {
          this.faultrecordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    getChangZhanList() {
      getchangzhanList().then(response=>{
        console.log('response',response)
        this.cznameList=response
      })
    },
    getBuJianList(){
      getbujianList().then(response=>{
        console.log('response',response)
        this.bjList=response
      })
    },
    getBuJian1List(){
      getbujian1List({"czid":this.queryParams.czid}).then(response=>{
        console.log('response',response)
        this.bjList=response
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        riqi: null,
        maintype: null,
        subtype: null,
        alarmlevel: null,
        quanxian: null,
        bjtype: null,
        bjid: null,
        bjpara: null,
        subnetid: null,
        czid: null,
        czname: null,
        bjname: null,
        bjno: null,
        msecond: null,
        eventtime: null,
        eventmsg: null,
        vollevel: null,
        baytype: null,
        bayid: null,
        bayname: null,
        devicename: null,
        voltageid: null,
        polystatetype: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.searchFlag = true;
      this.getList();
    },
    /** 周期搜索按钮 */
    handleQueryZhouQi() {
      this.queryParams.pageNum = 1;
      this.searchFlag = false;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加2020年1月故障记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFaultrecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改2020年1月故障记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFaultrecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFaultrecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除2020年1月故障记录编号为"' + ids + '"的数据项？').then(function() {
        return delFaultrecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ruoyi-yuce/faultrecord/export', {
        ...this.queryParams
      }, `faultrecord_${new Date().getTime()}.xlsx`)
    },
    /** 导出按钮操作 */
    handleExport1() {
      this.download('ruoyi-yuce/endstart/export', {
        ...this.queryParams
      }, `endstart_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
